// 专用于取普通员工的数据的服务

import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import {ApiServiceService} from './api-service.service';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {catchError, map, tap} from 'rxjs/operators';
import {Observable, of} from 'rxjs';
import {Admin, PunchRecord, OutRecord, LeaveRecord, BasicInformation} from '../domain/admin';



@Injectable({
  providedIn: 'root'
})
export class ServiceLogicService {
  private api_url;
  private headers;
  constructor(private http: Http, private apiService: ApiServiceService) {
    this.api_url = apiService.getUrl();
    this.headers = apiService.getHeaders();
  }
  judgeLogin(name: string, pwd: string): Promise<Admin[]> {
    const url = this.api_url +  '/admin' + '?admin_pwd=' + pwd + '&admin_name=' + name;
    console.log(url);
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Admin[])
      .catch(this.handleError);
  }
  private handleError(error: any): Promise<any> {
    console.log('An error occurred', error);
    return Promise.reject(error.message || error);
  }
  addPunchInRecord(punchRecord: PunchRecord) {
    const url = this.api_url + '/attendance_record';
    return this.http
      .post(url, JSON.stringify(punchRecord), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as PunchRecord)
      .catch(this.handleError);
  }
  getWorkNo(id: string) {
    const url = this.api_url + '/basic_info_worker' + '?admin_id=' + id;
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json())
      .catch(this.handleError);
  }
  getOutRecords(state: number, admin_id: string) {
    let url;
    if (state === 1) {
      url = this.api_url + '/out_appli_record' + '?state=' + state + '&&admin_id=' + admin_id;
    } else {
      url = this.api_url + '/out_appli_record' + '?state_ne=' + 1 + '&&admin_id=' + admin_id;
      console.log(url);
    }
    // const url = this.api_url + '/out_appli_record' + '?state=' + state;
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => <OutRecord[]> res.json() )
      .then(data => data)
      .catch(this.handleError);
  }
  addOutRecord(outRecord: OutRecord) {
    const url = this.api_url + '/out_appli_record';
    return this.http
      .post(url, JSON.stringify(outRecord), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as OutRecord)
      .catch(this.handleError);
  }
  deleteOutRecord(id: string) {
    const url = this.api_url + '/out_appli_record' + '/' + id;
    return this.http
      .delete(url, {headers: this.headers})
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  updateOutRecord(outRecord: OutRecord) {
    const url = this.api_url + '/out_appli_record' + '/' + outRecord.id;
    return this.http
      .put(url, JSON.stringify(outRecord), {headers: this.headers})
      .toPromise()
      .catch(this.handleError);
  }
  getLeaveRecords(state: number, admin_id: string) {
    console.log(state);
    let url;
    if (state === 1) {
      url = this.api_url + '/leave_appli_record' + '?state=' + state + '&&admin_id=' + admin_id;
    } else {
      url = this.api_url + '/leave_appli_record' + '?state_ne=' + 1 + '&&admin_id=' + admin_id;
    }
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => <LeaveRecord[]> res.json() )
      .catch(this.handleError);
  }
  deleteLeaveRecords(id: string) {
    const url = this.api_url + '/leave_appli_record' + '/' + id;
    return this.http
      .delete(url, {headers: this.headers})
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  addLeaveRecord(leaveRecord: LeaveRecord) {
    const url = this.api_url + '/leave_appli_record';
    return this.http
      .post(url, JSON.stringify(leaveRecord), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as LeaveRecord)
      .catch(this.handleError);
  }
  updateLeaveRecord(leaveRecord: LeaveRecord) {
    const url = this.api_url + '/leave_appli_record' + '/' + leaveRecord.id;
    return this.http
      .put(url, JSON.stringify(leaveRecord), {headers: this.headers})
      .toPromise()
      .catch(this.handleError);
  }
  // 选出所有其他人，已通过的外出请求
  getAllOutRecords(id: string) {
    const url = this.api_url + '/out_appli_record' + '?admin_id_ne=' + id + '&&state=2';
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => <OutRecord[]> res.json() )
      .catch(this.handleError);
  }
  // 选出所有其他人， 已通过的请假
  getAllLeaveRecords(id: string) {
    const url = this.api_url + '/leave_appli_record' + '?admin_id_ne=' + id + '&&state=2';
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => <LeaveRecord[]> res.json() )
      .catch(this.handleError);
  }
  getBasicInformation(id: string) {
    const url = this.api_url + '/basic_info_worker' + '?admin_id=' + id;
    console.log(url);
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => <BasicInformation>res.json())
      .catch(this.handleError);
  }
  getAdmin(state: string) {
    const url = this.api_url + '/admin' + '?admin_state_ne=' + state;
    console.log(url);
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => <Admin[]>res.json())
      .catch(this.handleError);
  }
  addAdmin(admin: Admin) {
    const url = this.api_url + '/admin';
    console.log(url);
    return this.http.post(url, JSON.stringify(admin), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Admin)
      .catch(this.handleError);
  }
  updateAdmin(admin: Admin) {
    const url = this.api_url + '/admin' + '/' + admin.id;
    console.log(url);
    return this.http.put(url, JSON.stringify(admin), {headers: this.headers})
      .toPromise()
      .catch(this.handleError);
  }
  deleteAdmin(id: string) {
    const url = this.api_url + '/admin' + '/' + id;
    return this.http
      .delete(url, {headers: this.headers})
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }
  getLeaveDaysLessThanThree(state: number) {
    let url;
    if (state === 1) {
      url = this.api_url + '/leave_appli_record' + '?state=' + 1 + '&days_lte=' + 3;
    } else {
      url = this.api_url + '/leave_appli_record' + '?state_ne=' + 1 + '&days_lte=' + 3;
    }
    return this.http
      .get(url, {headers: this.headers})
      .toPromise()
      .then(res => <LeaveRecord[]> res.json())
      .catch(this.handleError);
  }
  getLeaveDaysGreatThanThree(state: number) {
    let url;
    if (state === 1) {
      url = this.api_url + '/leave_appli_record' + '?state=' + 1 + '&days_gte=' + 4;
    } else {
      url = this.api_url + '/leave_appli_record' + '?state_ne=' + 1 + '&days_gte=' + 4;
    }
    return this.http
      .get(url, {headers: this.headers})
      .toPromise()
      .then(res => <LeaveRecord[]> res.json())
      .catch(this.handleError);
  }
  getOutDayLessThanThree(state: number) {
    let url;
    if (state === 1) {
      url = this.api_url + '/out_appli_record' + '?state=' + 1 + '&days_lte=' + 3;
    } else {
      url = this.api_url + '/out_appli_record' + '?state_ne=' + 1 + '&days_lte=' + 3;
    }
    return this.http
      .get(url, {headers: this.headers})
      .toPromise()
      .then(res => <OutRecord[]> res.json())
      .catch(this.handleError);
  }
  getOutDayGreatThanThree(state: number) {
    let url;
    if (state === 1) {
      url = this.api_url + '/out_appli_record' + '?state=' + 1 + '&days_gte=' + 4;
    } else {
      url = this.api_url + '/out_appli_record' + '?state_ne=' + 1 + '&days_gte=' + 4;
    }
    return this.http
      .get(url, {headers: this.headers})
      .toPromise()
      .then(res => <OutRecord[]> res.json())
      .catch(this.handleError);
  }
  approvalLeaveDaysLessThanThree(leaveRecord: LeaveRecord) {
    const url = this.api_url + '/leave_appli_record' + '/' + leaveRecord.id;
    console.log(url);
    return this.http.put(url, JSON.stringify(leaveRecord), {headers: this.headers})
      .toPromise()
      .catch(this.handleError);
  }
  // approvalLeaveDaysGreatThanThree(leaveRecord: LeaveRecord) {
  //   const url = this.api_url + '/leave_appli_record' + '/' + leaveRecord.id;
  //   console.log(url);
  //   return this.http.put(url, JSON.stringify(leaveRecord), {headers: this.headers})
  //     .toPromise()
  //     .catch(this.handleError);
  // }
  approvalOutDaysLessThanThree(outRecord: OutRecord) {
    const url = this.api_url + '/out_appli_record' + '/' + outRecord.id;
    console.log(url);
    return this.http.put(url, JSON.stringify(outRecord), {headers: this.headers})
      .toPromise()
      .catch(this.handleError);
  }
  updateBasicInformation(basicInformation: BasicInformation) {
    const url = this.api_url + '/basic_info_worker' + '/' + basicInformation.id;
    return this.http.put(url, JSON.stringify(basicInformation), {headers: this.headers})
      .toPromise()
      .catch(this.handleError);
  }
  // 选出所有员工已通过的请假记录
  getAllEmployeeOutRecords() {
    const url = this.api_url + '/out_appli_record' + '?state=' + 2;
    return this.http
      .get(url, {headers: this.headers})
      .toPromise()
      .then(res => <OutRecord[]> res.json())
      .catch(this.handleError);
  }
  getAllEmployeeLeaveRecord() {
    const url = this.api_url + '/leave_appli_record' + '?state=' + 2;
    return this.http
      .get(url, {headers: this.headers})
      .toPromise()
      .then(res => <LeaveRecord[]> res.json())
      .catch(this.handleError);
  }
  getAllAttendanceRecords() {
    const url = this.api_url + '/attendance_record';
    return this.http.get(url)
      .toPromise()
      .then(res => <LeaveRecord[]> res.json() )
      .catch(this.handleError);
  }
}
